package com.cf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cf.domain.ResponseResult;
import com.cf.domain.dto.UserDTO;
import com.cf.domain.pojo.PageQuery;
import com.cf.domain.pojo.Team;
import com.cf.domain.pojo.TeamMember;
import com.cf.domain.pojo.User;
import com.cf.domain.vo.TeamVO;
import com.cf.domain.vo.UserVO;
import com.cf.enums.UserRole;
import com.cf.exception.MyException;
import com.cf.service.ITeamMemberService;
import com.cf.service.ITeamService;
import com.cf.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {

    private final IUserService userService;

    private final ITeamService teamService;

    private final ITeamMemberService teamMemberService;

    @GetMapping("/team/{userId}")
    @ResponseBody
    public ResponseResult getUserTeam(@PathVariable Long userId){
        Team team = teamService.getByUserId(userId);
        return ResponseResult.sucess(team);
    }

    @GetMapping("/members/{userId}")
    @ResponseBody
    public ResponseResult getUserMembers(@PathVariable Long userId){
        List<TeamMember> data = teamMemberService.listByUserId(userId);
        return ResponseResult.sucess(data);
    }



    @PostMapping("/update")
    @ResponseBody
    public ResponseResult register(@RequestParam("name") String name,
                                   @RequestParam("phone") String phone,
                                   @RequestParam("email") String email,
                                   @RequestParam("area") String area, HttpSession session){
        try {
            UserVO user = (UserVO)session.getAttribute("user");
            UserDTO userDTO = new UserDTO();
            userDTO.setId(user.getId());
            userDTO.setName(name);
            userDTO.setPhone(phone);
            userDTO.setEmail(email);
            userDTO.setArea(Integer.valueOf(area));
            userDTO.setType(user.getType());
            int result = userService.update(userDTO);
            if(result>0){
                session.setAttribute("user", getUserVO(userDTO));
                return ResponseResult.sucess(null);
            }else{
                return ResponseResult.fail("系统开了小差！");
            }
        } catch (MyException e) {
            return ResponseResult.fail(e.getMessage());
        }
    }

    @PostMapping("/getTeam")
    @ResponseBody
    public ResponseResult getTeam(@RequestParam("id") Long id, HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        if(userVO.getUserRole() == UserRole.USER.getId()){
            throw new MyException("无操作数据权限！");
        }
        TeamVO teamVO = userService.getTeam(id);
        return ResponseResult.sucess(teamVO);
    }

    private UserVO getUserVO(UserDTO user) {
        UserVO userVO = new UserVO();
        userVO.setId(user.getId());
        userVO.setName(user.getName());
        userVO.setPhone(user.getPhone());
        userVO.setEmail(user.getEmail());
        userVO.setArea(user.getArea());
        userVO.setType(user.getType());
        return userVO;
    }

}
